package com.tuanbiao.presenter.setting;

import android.text.TextUtils;

import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.OnLifecycleEvent;

import com.tuanbiao.App;
import com.tuanbiao.R;
import com.tuanbiao.base.mvp.BasePresenter;
import com.tuanbiao.controllers.SettingController;

/**
 * @author abiao
 * @since 2022/5/5 17:25
 */
public class SettingLocationPresenter extends BasePresenter<SettingController.Model, SettingController.LocationView> implements SettingController.LocationPresenter {
    public SettingLocationPresenter(SettingController.Model model, SettingController.LocationView rootView) {
        super(model, rootView);
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
    private void init() {
        if (null != mModel.getSettingParams().getPseudoLonLat()) {
            mRootView.showDetails(mModel.getSettingParams().getEnablePseudoMode(), mModel.getSettingParams().getPseudoLonLat());
        } else {
            mRootView.showDetails(false, App.getInstance().getCurrentLocation());
        }
    }

    @Override
    public void save(boolean use, String latitude, String longitude) {
        if (TextUtils.isEmpty(latitude)) {
            mRootView.showMessage(R.string.setting_location_latitude_hint);
            return;
        }
        if (TextUtils.isEmpty(longitude)) {
            mRootView.showMessage(R.string.setting_location_longitude_hint);
            return;
        }
        mModel.savePseudoLocation(use, latitude, longitude, aBoolean -> {
            if (aBoolean) {
                mRootView.showMessage("保存成功！");
                mModel.refreshAuth();
            }
            else mRootView.showMessage("保存失败！");
        });
    }
}
